package com.cwt_taxi_xa.util;

import com.cwt_taxi_xa.model.GpsLocationInfo;

/**
 * 两地距离工具类
 * @author wlh
 */
public class DistanceUtil {

	private static final double EARTH_RADIUS = 6378137.0;

	/**
	 * 保存里程
	 * @param locationA	GPS位置A
	 * @param locationB	GPS位置B
	 */
//	public static void setMileage(GpsLocationInfo locationA, GpsLocationInfo locationB){
//		double distance = getMileage();
//		double myDistance = getDistance(locationA, locationB);
//		if(myDistance != 0.0){
//			myDistance = distance + myDistance;
//
//			SPutil.defaultEditor().putString("carcall_distance", String.valueOf(myDistance)).commit();
//		}
//	}

	/**
	 * 获取里程（单位：米）
	 * @return
	 */
//	public static double getMileage(){
//		String distance = SPutil.defaultPref().getString("carcall_distance", "0.0");
//		return Double.parseDouble(distance);
//	}

	/**
	 * 获取两地之间的距离 （单位：米）
	 * @param locationA	GPS位置A
	 * @param locationB	GPS位置B
	 * @return
	 */
	public static double getDistance(GpsLocationInfo locationA, GpsLocationInfo locationB){
		double latA = locationA.getLat();
		double lngA = locationA.getLng();
		double latB = locationB.getLat();
		double lngB = locationB.getLng();
		return getDistance(lngA, latA, lngB, latB);
	}

	/**
	 * 获取两地之间的距离 （单位：米）
	 * @param longitude1	经度1
	 * @param latitude1		纬度1
	 * @param longitude2	经度2
	 * @param latitude2		纬度2
	 * @return
	 */
	public static double getDistance(double longitude1, double latitude1,
									 double longitude2, double latitude2) {
		double Lat1 = rad(latitude1);
		double Lat2 = rad(latitude2);
		double a = Lat1 - Lat2;
		double b = rad(longitude1) - rad(longitude2);
		double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(Lat1) * Math.cos(Lat2)
				* Math.pow(Math.sin(b / 2), 2)));
		distance = distance * EARTH_RADIUS;
		distance = Math.round(distance * 10000) / 10000;
		return distance;
	}
	/**
	 * 获取两地之间的距离 （单位：米）
	 * @param longitude1	经度1
	 * @param latitude1		纬度1
	 * @param longitude2	经度2
	 * @param latitude2		纬度2
	 * @return
	 */
	public static double getDistance2(double longitude1, double latitude1,
									  double longitude2, double latitude2) {
		longitude1 = longitude1/1000000;
		latitude1 = latitude1/1000000;
		longitude2 = longitude2/1000000;
		latitude2 = latitude2/1000000;
		double Lat1 = rad(latitude1);
		double Lat2 = rad(latitude2);
		double a = Lat1 - Lat2;
		double b = rad(longitude1) - rad(longitude2);
		double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(Lat1) * Math.cos(Lat2)
				* Math.pow(Math.sin(b / 2), 2)));
		distance = distance * EARTH_RADIUS;
		distance = Math.round(distance * 10000) / 10000;
		return distance;
	}

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}
}
